package karaoke.servlet.Cliente;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import karaoke.excepcion.DAOExcepcion;
import karaoke.modelo.Cliente;
import karaoke.negocio.GestionClientes;




/**
 * Servlet implementation class ClienteEditarServlet
 */
@WebServlet("/ClienteEditarServlet")
public class ClienteEditarServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ClienteEditarServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String ab = request.getParameter("id");
		int a = Integer.parseInt(ab);
		
		GestionClientes cliente = new GestionClientes();
		try {
			Cliente vo = cliente.obtener(a);
			
			
			request.setAttribute("vo", vo);
		} catch (DAOExcepcion e) {
			System.out.println(e.getMessage());
			RequestDispatcher rd = request.getRequestDispatcher("error.jsp");
			rd.forward(request, response);
		}
		RequestDispatcher rd = request.getRequestDispatcher("Mantenimiento_Clientes_Editar.jsp");
		rd.forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		// ACTUALIZAR LA TABLA
		
		String cliente_Id = request.getParameter("id");
			
		String cliente_tipo = request.getParameter("txtTipo");
		
		String num = request.getParameter("txtNum");
		int cliente_num = Integer.parseInt(num);
		
		String cliente_nombre = request.getParameter("txtNombre");
		String paterno = request.getParameter("txtApePat");
		String materno = request.getParameter("txtApeMat");
		String cliente_correo = request.getParameter("txtCorreo");
		String cliente_pass= request.getParameter("txtPass");

		GestionClientes cliente = new GestionClientes();
		try {
			Cliente vo = new Cliente();
			vo.setCliente_Id(Integer.parseInt(cliente_Id));
			vo.setCliente_Tipo(cliente_tipo);
			vo.setCliente_Num(cliente_num);
			vo.setCliente_Nombre(cliente_nombre);
			vo.setCliente_ApePat(paterno);
			vo.setCliente_ApeMat(materno);
			vo.setCliente_Correo(cliente_correo);
			vo.setCliente_Pass(cliente_pass);
					
			cliente.actualizar(vo);
			response.sendRedirect(request.getContextPath() + "/Mantenimiento_Clientes_Buscar.jsp");
		} catch (DAOExcepcion e) {
			RequestDispatcher rd = request.getRequestDispatcher("error.jsp");
			rd.forward(request, response);
		}
		
	}

}
